﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace FastDeal.DoanhNghiep
{
    public partial class ChiTietKhuyenMai : System.Web.UI.Page
    {
        int ma;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 3)
            {
                Response.Redirect("~/Default.aspx");
            }
            if (!IsPostBack)
                LoadDuLieu();

        }

        /// <summary>
        /// Load danh sách dữ liệu
        /// </summary>
        private void LoadDuLieu()
        {
            try
            {
                ma = int.Parse(Request.QueryString["ID"].ToString());
                KhuyenMai km = new KhuyenMai();
                KhuyenMai_BUS db = new KhuyenMai_BUS();
                km = db.KhuyenMai_MaKhuyenMai(ma);
                lbMaDiaDiem.Text = km.MaKhuyenMai.ToString();
                lbTenDiaDiem.Text = km.TenKhuyenMai;
                lbMieuTa.Text = km.TomTat;
                lbGiaGoc.Text = km.GiaGoc.ToString() + " VNĐ";
                lbGiaKhuyenMai.Text = km.GiaKhuyenMai.ToString() + " VNĐ";
                lbBatDau.Text = km.ThoiGianBatDau.ToShortDateString();
                lbKetThuc.Text = km.ThoiGianKetThuc.ToShortDateString();
                lbTongLuotMua.Text = km.TongSoNguoiMua.ToString();
                lbDoanhThu.Text = (km.TongSoNguoiMua * km.GiaKhuyenMai).ToString() + " VNĐ";
                if (km.TinhTrang == 1)
                    lbTinhTrang.Text = "Đã Duyệt";
                else
                    lbTinhTrang.Text = "Chưa duyệt";

                //Lấy hình ảnh
                KhuyenMai_HinhAnh_BUS db1 = new KhuyenMai_HinhAnh_BUS();
                KhuyenMai_HinhAnh kmha = db1.LayDanhSachTheoMaKhuyenMai_KhuyenMai_HinhAnh(km.MaKhuyenMai)[0];
                HinhAnh_BUS db2 = new HinhAnh_BUS();
                HinhAnh ha = new HinhAnh();
                ha = db2.LayThongTin_HinhAnh(kmha.MaHinhAnh)[0];
                lbHinhAnh.ImageUrl = ha.DuongDan;


                //kiểm tra tình trạng hết hàng
                if (km.SoLuongToiDa >= km.TongSoNguoiMua && km.ThoiGianKetThuc > DateTime.Now)
                {
                    lbHetHang.Text = "Cháy Hàng";
                }
                else if (km.SoLuongToiDa >= km.TongSoNguoiMua && km.ThoiGianKetThuc < DateTime.Now)
                {
                    lbHetHang.Text = "Hết Khuyến Mãi";
                }
                else
                {
                    lbHetHang.Text = "Đang Hoạt Động";
                }

            }
            catch
            {
                Response.Redirect("~/DoanhNghiep/DanhSachKhuyenMai.aspx");
            }
        }
    }
}